import {Page, NavController, NavParams} from 'ionic-angular';
import {Observable}       from 'rxjs/Observable';
import {ImgPathPipe} from '../../pipes/img-path';
import {CurrencyCnyPipe} from '../../pipes/currency-cny';
import {Product} from '../../models/product';

import {ProductService}    from '../../services/product';

@Page({
  templateUrl: 'build/pages/product/product-detail.html',
  providers: [ProductService],
  pipes: [ImgPathPipe,CurrencyCnyPipe]
})
export class ProductDetail {
    errorMessage: any;
    private productId: string;
    private product: Product;
    private isCollected: boolean;
    private qty: number = 0;

    constructor(nav: NavController, params: NavParams, private _productService: ProductService) {
        let me = this;
        me.productId = params.data;
        me._productService.getDetail(me.productId).subscribe(
            json => {me.product = json.data; me.isCollected = json.isCollected;} ,
            error => me.errorMessage = <any>error
        );
    }

    collect (product) {
        let me = this;
        me._productService.collect(this.productId).subscribe(
            json => {
                console.log(json);
                if (json.code > 0) {
                    me.isCollected = json.data;
                    console.log('success');
                }
            },
            error => me.errorMessage = <any>error
        );
     }
}
